package com.gcxy.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gcxy.dto.Fence.InsertDto;
import com.gcxy.dto.Fence.UpdateDto;
import com.gcxy.entity.Fence;
import com.gcxy.entity.Pen;
import com.gcxy.entity.Ranch;
import com.gcxy.mapper.FenceMapper;
import com.gcxy.mapper.PenMapper;
import com.gcxy.mapper.RanchMapper;
import com.gcxy.service.FenceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gcxy.utils.R;
import com.gcxy.vo.Fence.FenceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wlp
 * @since 2023-12-19
 */
@Service
public class FenceServiceImpl extends ServiceImpl<FenceMapper, Fence> implements FenceService {
    @Autowired
    private FenceMapper fenceMapper;
    @Autowired
    private PenMapper penMapper;
    @Autowired
    private RanchMapper ranchMapper;

    @Override
    public R fenceInsert(InsertDto requestBody) {
        LambdaQueryWrapper<Fence> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Fence::getFenceId, requestBody.getPenId())
                .eq(Fence::getPenId, requestBody.getPenId())
                .eq(Fence::getFenceName, requestBody.getFenceName());
        //判断牧场名是否存在（前端）
//        LambdaQueryWrapper<Ranch> queryWrapper2 = new LambdaQueryWrapper<>();
//        queryWrapper2.eq(Ranch::getRanchId, requestBody.getRanchId())
//                .eq(Ranch::getRanchState, 1);
//        Ranch ranch = ranchMapper.selectOne(queryWrapper2);
//        if (ranch == null) {
//            return R.Failed(0, "牧场名不存在");
//        }
        //判断圈舍是否不存在（前端）
//        LambdaQueryWrapper<Pen> queryWrapper1 = new LambdaQueryWrapper<>();
//        queryWrapper1.eq(Pen::getPenId, requestBody.getPenId())
//                .eq(Pen::getRanchId, requestBody.getRanchId())
//                .eq(Pen::getPenState, 1);
//        Pen pen = penMapper.selectOne(queryWrapper1);
//        if (pen == null) {
//            return R.Failed(0, "圈舍名不存在");
//        }
        Fence fence = fenceMapper.selectOne(queryWrapper);
        if (fence != null) {
            return R.Failed(0, "围栏名已存在");
        }
        Fence fence1 = Fence.builder()
                .fenceName(requestBody.getFenceName())
                .fenceState(1)
                .fenceAmount(1)
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .penId(requestBody.getPenId())
                .ranchId(requestBody.getRanchId())
                .build();
        int insert = fenceMapper.insert(fence1);
        if (insert > 0) {
            //圈舍增加存栏量
//            Pen pen1 = Pen.builder()
//                    .penAmount(pen.getPenAmount() + fence1.getFenceAmount())
//                    .penId(requestBody.getPenId())
//                    .build();
//            int i = penMapper.updateById(pen1);
//            if (i > 0) {
            //牧场增加存栏量
//                Ranch ranch1 = Ranch.builder()
//                        .ranchId(requestBody.getRanchId())
//                        .ranchAmount(ranch.getRanchAmount() + pen1.getPenAmount())
//                        .build();
//                int i1 = ranchMapper.updateById(ranch1);
//                if (i1 > 0) {
                    return R.Success();
//                }
            }
//        }
        return R.Failed(0, "新增围栏失败");
    }

    @Override
    public R fencePage(FenceVo requestBody) {
        Page<FenceVo> page = new Page<>(requestBody.getPageNumber(), requestBody.getPageSize());
        QueryWrapper<FenceVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(requestBody.getRanchName() != null && !"".equals(requestBody.getRanchName()), "r.ranch_name", requestBody.getRanchName())
                .like(requestBody.getPenName() != null && !"".equals(requestBody.getPenName()), "p.pen_name", requestBody.getPenName())
                .like(requestBody.getFenceName() != null && !"".equals(requestBody.getFenceName()), "f.fence_name", requestBody.getFenceName())
                .eq(requestBody.getFenceState() != null, "f.fence_state", requestBody.getFenceState())
                .orderByDesc("create_time");
        fenceMapper.selectFencePage(page, queryWrapper);
        return R.Success(page);
    }

    @Override
    public R fenceUpdate(UpdateDto requestBody) {
        LambdaQueryWrapper<Fence> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Fence::getRanchId, requestBody.getRanchId())
                .eq(Fence::getPenId, requestBody.getPenId())
                .eq(Fence::getFenceName, requestBody.getFenceName());
        Fence fence = fenceMapper.selectOne(queryWrapper);
        if (fence != null && !fence.getFenceId().equals(requestBody.getFenceId())) {
            return R.Failed(0, "围栏名称已存在");
        }
//        Fence fence1 = Fence.builder()
//                .fenceId(requestBody.getFenceId())
//                .build();
//        //最开始（没有修改）之前的值
//        Fence fence3 = fenceMapper.selectById(fence1);

        //最新的圈舍
//        LambdaQueryWrapper<Pen> queryWrapper1 = new LambdaQueryWrapper<>();
//        queryWrapper1.eq(Pen::getRanchId, requestBody.getRanchId())
//                .eq(Pen::getPenId, requestBody.getPenId());
//        //最新的pen值
//        Pen pen = penMapper.selectOne(queryWrapper1);
//        if (pen == null) {
//            return R.Failed(0, "圈舍不存在");
//        }
        Fence fence2 = Fence.builder()
                .updateTime(LocalDateTime.now())
                .fenceId(requestBody.getFenceId())
                .penId(requestBody.getPenId())
                .fenceName(requestBody.getFenceName())
                .build();
        int i = fenceMapper.updateById(fence2);
        if (i > 0) {
            //判断两个圈舍是否相同来看是否增减存栏量
//            if (pen.getPenId() == fence3.getPenId()) {
//                return R.Success();
//            }
//            LambdaQueryWrapper<Pen> queryWrapper2 = new LambdaQueryWrapper<>();
//            queryWrapper2.eq(Pen::getPenId, fence3.getPenId());
//            Pen pen1 = penMapper.selectOne(queryWrapper2);
//            Pen pen2 = Pen.builder()
//                    .penId(fence3.getPenId())
////                    .penAmount(pen1.getPenAmount() - fence3.getFenceAmount())
//                    .build();
//            int i1 = penMapper.updateById(pen2);
//            if (i1 > 0) {
//                Pen pen3 = Pen.builder()
//                        .penId(pen.getPenId())
////                        .penAmount(pen.getPenAmount() + fence3.getFenceAmount())
//                        .build();
//                int i2 = penMapper.updateById(pen3);
//                if (i2 > 0) {
                    return R.Success();
//                }
//            }
        }
        return R.Failed(0, "修改失败");
    }

    @Override
    public R fenceUpdateState(Integer fenceId, Integer fenceState) {
        //前端点击选择
//        Fence fence=fenceMapper.selectById(fenceId);
//        if(fence==null){
//            return R.Failed(0,"围栏不存在");
//        }
        Fence fence1=Fence.builder()
                .fenceId(fenceId)
                .fenceState(fenceState)
                .build();
        int i = fenceMapper.updateById(fence1);
        if (i>0){
            return R.Success();
        }
        return R.Failed(0,"修改失败");
    }
}
